/*
 * Created on Sep 19, 2005 1:01:18 PM
 * 
 * $Id$
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package tripleo.fmsys;

import java.io.*;
import java.nio.ByteBuffer;
import java.util.HashMap;

public class default_fmcmpt implements fmcmpt{
	fmsystem syst;
	String addr;
	
	public default_fmcmpt(String aAddr, fmsystem aSyst) {
		addr = aAddr;
		syst = aSyst;
	}

	/* (non-Javadoc)
	 * @see tripelo.fmsys.fmcmpt#call(java.lang.String)
	 */
	public FMResponse call(String method) throws IOException {
		return call(method, new HashMap());
	}

	public FMResponse call(String aMethod, HashMap<String, Object> aMap) throws IOException {
		FMRequest req = syst.request(Requests.CALL);
		ByteBuffer x = ByteBuffer.allocateDirect(4096);
		x.putInt(0xDEADCAFE);
		x.putInt(req.code().ordinal());
		for (String key : aMap.keySet()) {
			Object value = aMap.get(key);
			ByteArrayOutputStream baos;
			DataOutputStream dos = new DataOutputStream(baos=new ByteArrayOutputStream());
			dos.writeUTF(key);
			dos.writeUTF(value.toString());
			x.put(baos.toByteArray());
		}
		req.write(x);
		return req.send();
	}
}
